# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:  smart-meter-iot-test-console
# FileName:     getSign.py
# Author  :
# Datetime:     2023/4/7 23:46
# Description： 获取供应商秘钥，方便识别区分不同的设备。
# -----------------------------------------------------------------------------------
import calendar
import time
from common.util.mySqlUtil.mySqlPool import select_sql
from common.util.configUtil.configUtil import ConfigUtil
from common.util.encryptionUtil.crc16Util import crc16_modbus
from common.util.jsonUtl.jsonUtil import JsonUtil

jsonUtil = JsonUtil()
configUtil = ConfigUtil()


def get_sign(deviceName: str, timestamp: str) -> str:
    """
    通过组合的字符串获取crc16_modbus校验码
    :return: 返回crc16_modbus检验码
    """
    # 获取供应商秘钥
    if deviceName.upper().startswith("C") or deviceName.upper().startswith("V"):
        selectVendorKeySql = jsonUtil.read_sql("collectorVendorKey").format(deviceName)
    else:
        selectVendorKeySql = jsonUtil.read_sql("meterVendorKey").format(deviceName)
    # 从数据库获取供应商秘钥
    vendorKey = select_sql(selectVendorKeySql)[0][0]
    content = f'{deviceName}{configUtil.get_productkey()}{timestamp}{vendorKey}'
    # 加密
    getSign = crc16_modbus(content)
    return getSign


if __name__ == '__main__':
    print(get_sign("V10230190000", str(calendar.timegm(time.gmtime()))))
